package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import com.swlz.commons.DBUtil;
import com.swlz.entity.MallCart;
import com.swlz.vo.MallCartVO;
/**
 * <pre>
 * 数据访问对象
 * 商城购物车
 * TableName : MALL_CART
 * ClassName : com.swlz.dao.MallCartDAO
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-29 12:16:00
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class MallCartDAO {

	/**
	 * 添加一个商城购物车
	 * 包含所有字段
	 * @param mallCart - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(MallCart mallCart) throws SQLException {
		if(mallCart.getCartId()==null) {
			mallCart.setCartId(DBUtil.nextId());
		}
		return DBUtil.insert(mallCart);
	}
 
	
	/**
	 * 添加一个商城购物车
	 * 只包含非null的字段
	 * @param mallCart - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(MallCart mallCart) throws SQLException {
		if(mallCart.getCartId()==null) {
			mallCart.setCartId(DBUtil.nextId());
		}
		return DBUtil.insertSelective(mallCart);
	}
	

	/**
	 * 根据主键更新一个商城购物车
	 * 包含所有字段
	 * @param mallCart - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(MallCart mallCart) throws SQLException {
		return DBUtil.update(mallCart);
	}
	
	/**
	 * 根据主键更新一个商城购物车
	 * 只包含非null的字段
	 * @param mallCart - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(MallCart mallCart) throws SQLException {
		return DBUtil.updateSelective(mallCart);
	}
	
	/**
	 * 根据主键删除一个商城购物车
	 * @param cartId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long cartId) throws SQLException  {
		return DBUtil.execute("DELETE FROM MALL_CART WHERE CART_ID=?", cartId);
	}
	
	public int delete(Long userId,List<Long> productIds)throws SQLException {
		String ids = productIds.toString();
		ids = ids.substring(1, ids.length()-1);
		return DBUtil.execute("DELETE FROM mall_cart WHERE USER_ID="+userId+" AND PRODUCT_ID in ("+ids+")");
	}
	
	/**
	 * 根据主键获取一个商城购物车
	 * @param cartId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallCart selectById(Long cartId) throws SQLException  {
		return DBUtil.get("SELECT * FROM MALL_CART WHERE CART_ID=?", MallCart.class ,cartId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param cartId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallCart selectByModel(MallCart mallCart)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM MALL_CART WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallCart,sql,params);
		return DBUtil.get(sql.toString(), MallCart.class, params.toArray());
	}
	
	
	public List<MallCartVO> selectByUserId(Long userId)throws SQLException {
		String sql = "SELECT a.*,b.PRODUCT_NAME,b.PRODUCT_PRICE_1,b.PRODUCT_PRICE_2,PRODUCT_IMGS from mall_cart a INNER JOIN mall_product b ON a.PRODUCT_ID=b.PRODUCT_ID AND a.USER_ID=? ORDER BY a.CART_TIME desc";
		return DBUtil.list(sql, MallCartVO.class, userId);
	}
	/**
	 * 根据模型中的非空字段查询
	 * @param cartId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<MallCart> selectByModel(MallCart mallCart,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM MALL_CART WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallCart,sql,params);
		sql.append(" ORDER BY CART_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), MallCart.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param cartId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(MallCart mallCart)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM MALL_CART WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallCart,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param cartId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private void where(MallCart mallCart,StringBuffer sql,List<Object> params) {
		if(mallCart==null){
			return;
		}

		if(mallCart.getCartId()!=null) {
			sql.append(" AND CART_ID=?");
			params.add(mallCart.getCartId());
		}

		if(mallCart.getUserId()!=null) {
			sql.append(" AND USER_ID=?");
			params.add(mallCart.getUserId());
		}

		if(mallCart.getProductId()!=null) {
			sql.append(" AND PRODUCT_ID=?");
			params.add(mallCart.getProductId());
		}

		if(mallCart.getCartNum()!=null) {
			sql.append(" AND CART_NUM=?");
			params.add(mallCart.getCartNum());
		}

		if(mallCart.getCartTime()!=null) {
			sql.append(" AND CART_TIME=?");
			params.add(mallCart.getCartTime());
		}

	}
}